package cn.hahah.module.blog.service.setting;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.hahah.module.blog.controller.admin.setting.vo.*;
import cn.hahah.module.blog.dal.dataobject.setting.SettingDO;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.util.object.BeanUtils;

import cn.hahah.module.blog.dal.mysql.setting.SettingMapper;

import static cn.hahah.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.hahah.module.blog.enums.ErrorCodeConstants.*;

/**
 * 博客系统设置 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class SettingServiceImpl implements SettingService {

    @Resource
    private SettingMapper settingMapper;

    @Override
    public Long createSetting(SettingSaveReqVO createReqVO) {
        // 插入
        SettingDO setting = BeanUtils.toBean(createReqVO, SettingDO.class);
        settingMapper.insert(setting);
        // 返回
        return setting.getId();
    }

    @Override
    public void updateSetting(SettingSaveReqVO updateReqVO) {
        // 校验存在
        validateSettingExists(updateReqVO.getId());
        // 更新
        SettingDO updateObj = BeanUtils.toBean(updateReqVO, SettingDO.class);
        settingMapper.updateById(updateObj);
    }

    @Override
    public void deleteSetting(Long id) {
        // 校验存在
        validateSettingExists(id);
        // 删除
        settingMapper.deleteById(id);
    }

    private void validateSettingExists(Long id) {
        if (settingMapper.selectById(id) == null) {
            throw exception(SETTING_NOT_EXISTS);
        }
    }

    @Override
    public SettingDO getSetting(Long id) {
        return settingMapper.selectById(id);
    }

    @Override
    public PageResult<SettingDO> getSettingPage(SettingPageReqVO pageReqVO) {
        return settingMapper.selectPage(pageReqVO);
    }

}